package com.project.api.ym.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.api.common.ApiResponse;
import com.project.api.common.auth.LoginToken;
import com.project.api.ym.dao.AdminDao;
import com.project.api.ym.model.Admin;
import com.project.api.ym.model.Roles;
import com.project.api.ym.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BaseController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private final String pf="ym";
    private final String vpf="verify/ym";
    @Autowired
    public AdminService adminService;
    @Autowired
    public AdminDao adminDao;

    @RequestMapping(value=pf+"/login",method = RequestMethod.POST)
    @ResponseBody
    public String Login(HttpServletRequest request, Model model,
                        @RequestBody JSONObject json){
        String username = (String) json.getOrDefault("username",null);
        String password = (String) json.getOrDefault("password",null);
        HttpSession session=request.getSession();
        JSONObject obj=new JSONObject();
        String sid=session.getId();
//        System.out.println("login_sid::"+sid);
//        int userId=(int)session.getAttribute("userId");
        String aid =adminService.Login(username,password);
        String token=LoginToken.Set(sid,aid);
        if (token!=null){
            session.setAttribute(LoginToken.NAME,token);
            session.setAttribute(LoginToken.ADMIN,aid);
            obj.put("token",token);
            return ApiResponse.Ok("success", obj);
        }
        return ApiResponse.Fail("fail");
    }


    @RequestMapping(value=vpf+"/admininfo",method = RequestMethod.GET)
    @ResponseBody
    public String AdminInfo(HttpServletRequest request, Model model,
                            @RequestParam(value="token",required = true) String  token){
        HttpSession session=request.getSession();
        String sid=session.getId();
//        if (!token.equals(LoginToken.Get())) return ApiResponse.Fail("Access Violation");
//        LoginToken.reflesh();
//        String a = LoginToken.Uid();
        long aid = Long.valueOf((String) session.getAttribute(LoginToken.ADMIN));
//        long uid = (long) json.getOrDefault("token",null);
//        JSONObject obj=new JSONObject();
        Admin admin =adminService.GetAdminInfo(aid);
        if(admin == null) return ApiResponse.Fail("fail");
        session.setAttribute("admin_info",admin);
        return ApiResponse.Ok("success",admin);
    }

    @RequestMapping(value=vpf+"/alladmin",method = RequestMethod.GET)
    @ResponseBody
    public String AllAdmin(HttpServletRequest request, Model model,@RequestParam(value="name",required = true) String  name){
        List<Admin> admins= adminDao.GetAllAdminByName(name);
        return ApiResponse.Ok("success",admins);
    }

    @RequestMapping(vpf+"/logout")
    @ResponseBody
    public String logout(HttpSession session, SessionStatus sessionStatus){
        session.invalidate();
        sessionStatus.setComplete();
        return ApiResponse.Ok("success",null);
    }
}
